package br.com.vilababy.sigvb.conversores;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.vilababy.sigvb.modelo.Aluno;

@FacesConverter(value="alunoConverter")
public class AlunoConverter implements Converter {

	/**
	 * @author Diego Patrick da Silva Santos
	 */
	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
		if (value != null && !value.isEmpty()) {
			Aluno aluno = new Aluno();
			aluno.setId(Long.parseLong(value));
			return aluno;
	    }
	    return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
		if (value instanceof Aluno) {
	        Aluno aluno = (Aluno) value;
	        if (aluno != null && aluno instanceof Aluno && aluno.getId() != null) {
	        	uiComponent.getAttributes().put( aluno.getId().toString(), aluno);
	            return aluno.getId().toString();
	        }
	    }
	    return "";
	}
	
}
